<template>
    <div class="control list_container flex both-100">
        <div class="content_container">
            <!-- 搜索 -->
            <el-form ref="searchForm" :model="searchMap">
                <Breadcrumb :title="pageTitle" />
                <div class="search_wrapper flex-space-between el-form--inline" style="margin-top: 20px;">
                    <div class="search flex">
                        <el-form-item prop="messageTemplateSubject" size="small">
                            <span class="label-tip" style="width: 160px;">通知信息主题：</span>
                            <el-input v-model="searchMap.messageTemplateSubject" placeholder="请输入通知信息主题"></el-input>
                        </el-form-item>
                        <!-- <el-form-item prop="dangerousSourceName" size="small">
                            <span class="label-tip" style="width: 160px;">报警归属设备：</span>
                            <el-input v-model="searchMap.dangerousSourceName" placeholder="请输入报警归属设备"></el-input>
                        </el-form-item> -->
                    </div>
                    <div class="edit">
                        <el-form-item class="flex">
                            <el-button @click="handleSearch()" class="btn_primary el-icon-search">查询</el-button>
                            <el-button @click="handleReset()" class="btn_cancel el-icon-refresh-left">重置</el-button>
                            <el-button @click="handleAdd()" class="btn_primary el-icon-plus">新增</el-button>
                        </el-form-item>
                    </div>

                </div>
            </el-form>
            <div class="table_container">
                <!-- 表格 -->
                <el-table :data="dataList" v-loading="loading" stripe class="per_table" style="width: 100%"
                    :height="winTableH">
                    <el-table-column type="index" width="50" label="序号" align="center" />
                    <el-table-column prop="messageTemplateSubject" label="通知信息主题"></el-table-column>
                    <!-- <el-table-column prop="riskControlMeasuresLevel" label="报警归属设备" align="center">
                        <template slot-scope="scope">
                                    <dict-tag :options="dict.type.alarm_attribution_device" :value="scope.row.riskControlMeasuresLevel" />
                                </template>
                    </el-table-column> -->
                    <el-table-column prop="messageTemplateContent" label="通知信息内容" align="center"></el-table-column>
                    <el-table-column prop="enableStatus" label="状态" align="center">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.enableStatus === '1'" type="success">启用</el-tag>
                            <el-tag v-else type="danger">禁用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="280">
                        <template slot-scope="scope">
                            <div>
                                <span type="text" class="tag_primary" size="small"
                                    @click="handleSelect(scope.row)">查看</span>
                                <span type="text" class="marginl-10 tag_primary" size="small"
                                    @click="handleEdit(scope.row)">编辑</span>
                                <span type="text" class="marginl-10 tag_warning" size="small"
                                    @click="handleDelete(scope.row)">删除</span>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- 分页 -->
            <Pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize"
                @change="getList"></Pagination>
        </div>
  
        <!-- 弹窗 详情 -->
        <el-dialog class="m_dialog" :title="title" :visible.sync="dialogVisible" width="800" append-to-body
            :before-close="() => { dialogVisible = false }" :close-on-click-modal="false">
            <div class="content">
                <el-form ref="form" :model="formModel" size="small" :rules="formRules" label-width="80px"
                    :disabled="title === '通知信息详情'">
                    <el-row :gutter="20">
                        <el-col :span="24">
                            <el-form-item label="通知信息主题" label-width="120px" prop="messageTemplateSubject">
                                <el-input v-model="formModel.messageTemplateSubject" placeholder="请输入"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="24">
                            <el-form-item label="报警归属设备" label-width="120px" prop="riskControlMeasuresLevel">
                                <el-select v-model="formModel.riskControlMeasuresLevel" placeholder="请选择"
                                    style="width: 100%;">
                                    <el-option v-for="(item, index) in dict.type.alarm_attribution_device" :label="item.label"
                                        :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="24">
                            <el-form-item label="状态" label-width="120px" prop="enableStatus">
                                <template>
                                    <el-radio v-model="formModel.enableStatus" label="1">启用</el-radio>
                                    <el-radio v-model="formModel.enableStatus" label="0">禁用</el-radio>
                                </template>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="通知信息内容" label-width="120px" prop="messageTemplateContent">
                                <el-input v-model="formModel.messageTemplateContent" placeholder="请输入" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注" label-width="120px" prop="remark">
                                <el-input v-model="formModel.remark" placeholder="请输入" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer" v-show="title === '通知信息详情'">
                <el-button @click="() => { dialogVisible = false }" class="btn_cancel">关 闭</el-button>
            </div>
            <div slot="footer" class="dialog-footer" v-show="title !== '通知信息详情'">
                <el-button @click="() => { dialogVisible = false }" class="btn_cancel">取 消</el-button>
                <el-button type="primary" @click="submitFrom()" class="btn_primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
  </template>
  <script>
  import { messageTemplate, messageTemplateList } from '../api/index'
  
  export default {
    name:'emergencyRanks',
    dicts: ['alarm_attribution_device'],
    data() {
        return {
            pageTitle: '通知信息模板',
            loading: false,
            total: 10,
            title: '通知信息详情',
            dialogVisible: false,
            searchMap: {
                pageNum: 1,
                pageSize: 10,
            },
            dataList: [
            ],
            // 提交表单数据
            formModel: {},
            reqType: 'post',
            formRules: {
                messageTemplateSubject: {
                    required: true,
                    message: "通知信息主题不能为空",
                    trigger: "change",
                },
                // riskControlMeasuresLevel: {
                //     required: true,
                //     message: "报警归属设不能为空",
                //     trigger: "change",
                // },
                messageTemplateContent: {
                    required: true,
                    message: "通知信息内容不能为空",
                    trigger: "change",
                },
            },
        }
    },
    methods: {
        submitFrom() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                  messageTemplate(this.formModel, this.reqType).then(res => {
                        if (200 === res.code) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                            });
                            this.dialogVisible = false
                            this.formModel = {}
                            this.getList()
                        }
                    });
                } else {
                    return false;
                }
            });
        },
        //删除
        handleDelete(row) {
            this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
              messageTemplate([row.messageTemplateId], 'delete').then(res => {
                    if (200 === res.code) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList()
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 查询
        handleSearch() {
            this.getList()
        },
        // 重置
        handleReset() {
            this.searchMap = {
                pageNum: 1,
                pageSize: 10,
            }
            this.getList()
        },
        //新增打开
        handleAdd() {
            this.title = "新增通知信息"
            this.reqType = 'post'
            if (this.$refs.form) {
                this.$refs.form.resetFields()
            }
            this.formModel = {}
            this.dialogVisible = true;
        },
        //编辑
        handleEdit(row) {
            this.title = "编辑通知信息"
            this.formModel = row
            this.reqType = 'put'
            this.dialogVisible = true;
        },
        //查看
        handleSelect(row) {
            this.title = "通知信息详情"
            this.formModel = row
            this.reqType = 'select'
            this.dialogVisible = true;
        },
        // 页面数据
        async getList() {
            const { code, rows, total } = await messageTemplateList(this.searchMap)
            if (code === 200) {
                this.loading = false
                this.dataList = rows
                this.total = total
            }
        },
    },
    mounted() {
      this.getList()
    },
  }
  </script>
  <style lang="less">
  
  
  .control {
    .el-table__cell {
        .cell {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    }
  }</style>
  <style lang="less" scoped>
  .list_container {
    flex-direction: column;
    background: url("@/assets/images/home/content.png") no-repeat;
    background-size: cover;
  
    .content_container {
        padding: 15px 20px;
        overflow-y: scroll;
    }
  
    .table_container {
        margin-bottom: 25px;
    }
  
    .pagination_container {
        display: flex;
        justify-content: space-between;
        height: 32px;
    }
  }
  </style>